<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\ModelHasPermission;
use App\Models\SystemUser;
use Illuminate\Http\Request;
use App\Models\Module;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Traits\HasPermissions;

class RoleHasPermissionController extends Controller
{
    //
    use ResponseData;
    public function roleGetPermission(Request $request){
        $data = Module::query()->with('permission')->get();
        return $this->success($data);
    }
    //当前角色拥有的权限
    public function roleOwnerPermission(Request $request){
        $id = $request->input('id','');
        $role = Role::query()->with(['permissions'=>function($query){
            $query->select('id','route','zh_name','name','module_name','sort','icon','guard_name','type');
        }])
            ->when($id,function ($query)use($id){
                $query->where('id',$id);
            })
            ->select('id','zh_name','name')
            ->first();
        return $this->success($role);
    }
    //角色分配权限
    public function roleHasPermission(Request $request){

        $role = Role::query()->find($request->id);
        $permission = $this->getPermissionId($request);
        $permissions = Permission::query()->whereIn('id',$permission)->get();
        $data = $role->syncPermissions($permissions);
        return $this->success($data);
    }
    //角色删除权限
    public function roleResolvePermission(Request $request){
        $role = Role::query()->find($request->id);
        $permission = $this->getPermissionId($request);
        $permissions = Permission::query()->whereIn('id',$permission)->get();
        $data = $role->revokePermissionTo($permissions);
        return $this->success($data);
    }
    //获取权限id
    protected function getPermissionId($request){
        if(is_string($request->permissions)){
            $permission = json_decode($request->permissions,true);
        }else{
            $permission = $request->permissions;
        }
        return $permission;
    }
    //获取当前用户所有权限
    public function getUserAllPermission(Request $request){

        $user = SystemUser::query()->find($request->id);
        if(!$user){
            return $this->failed('用户不存在!',201);
        }
        $arr = ModelHasPermission::query()
            ->where('model_id',$request->id)
            ->pluck('permission_id');
        $permissions = Permission::query()->whereIn('id',$arr)
            ->orderByDesc('sort')->get();
        return $this->success($permissions);
    }
    //编辑当前用户权限
    public function editUserPermission(Request $request){
        $user = SystemUser::query()->find($request->id);
        if(!$user){
            return $this->failed('用户不存在!',201);
        }
        $owner = $user->getAllPermissions();
//        $allPermission = Permission::query()->get();
        return $this->success(compact('owner'));
    }
    //分配权限
    public function updateUserPermissions(Request $request){

        if($request->filled('permission')){
            if(is_string($request->permission)){

                $permission_id = json_decode($request->permission);
            }else{

                $permission_id = $request->permission;
            }
        }else{

            return $this->failed('permission不能为空',201);
        }

        $user = SystemUser::query()->find($request->id);
        if(!$user){
            return $this->failed('用户不存在!',201);
        }
        $user->syncPermissions($permission_id);
        return $this->success('权限分配成功');
    }
}
